﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using System.Collections.Generic;

	public partial class Settings
	{
		private static readonly Dictionary<string, string> Values = new Dictionary<string, string>();

		/// <summary>
		/// Initializes the specified settings.
		/// </summary>
		/// <param name="settings">The settings.</param>
		public void Init(IList<Domain.Setting> settings)
		{
			lock (Values)
			{
				if (Values.Count > 0)
				{
					return;
				}

				foreach (var setting in settings)
				{
					Values.Add(setting.Key, setting.Value);
				}
			}
		}

		/// <summary>
		/// Rebuilds the specified settings.
		/// </summary>
		/// <param name="settings">The settings.</param>
		public void Rebuild(IList<Domain.Setting> settings)
		{
			lock (Values)
			{
				Values.Clear();

				foreach (var setting in settings)
				{
					Values.Add(setting.Key, setting.Value);
				}
			}
		}

		/// <summary>
		/// Gets the setting by key.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <returns></returns>
		public static string GetByKey(string key)
		{
			return Values.ContainsKey(key) ? Values[key] : key;
		}
	}
}